package com.gxa.controller;

import com.gxa.pojo.Alarm;
import com.gxa.pojo.Resource;
import com.gxa.pojo.ResourceVO;
import com.gxa.service.ResourceService;
import com.gxa.utils.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.metrics.StartupStep;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags="资源管理")
@RequestMapping("Resource")
@Repository
public class ResourceController {
    @Autowired
    ResourceService resourceService;


    @ApiOperation(value = "分页显示全部数据",httpMethod ="GET",response =ResponseData.class )
    @RequestMapping("/showResource")
    @ResponseBody
    public ResponseData showResource(ResourceVO resourceVO){
        ResponseData responseData=new ResponseData();
        responseData.setCount(resourceService.getCount());
        List<Resource> resources=resourceService.showResource(resourceVO);
        for(Resource resource:resources){
            System.out.println(resource);
        }
        responseData.setData(resources);
        return responseData;
    }


    @ApiOperation(value = "按条件查询数据",httpMethod ="GET",response =ResponseData.class )
    @RequestMapping("/selectByCondition")
    @ResponseBody
    public ResponseData selectByCondition(ResourceVO resourceVO){
        ResponseData responseData=new ResponseData();
        responseData.setCount(resourceService.getCount());
        responseData.setData(resourceService.selectByCondition(resourceVO));
        return responseData;
    }



    @ApiOperation(value = "根据资源名称删除数据",httpMethod ="GET",response =ResponseData.class )
    @RequestMapping("/deleteByResourceName")
    @ResponseBody
    public ResponseData deleteByResourceName(@ApiParam(value = "资源名称",required = true)String resourceName){
        ResponseData responseData=new ResponseData();
        responseData.setData(resourceService.deleteByResourceName(resourceName));
        return responseData;

    }

    @ApiOperation(value = "根据资源名称组成的数组删除数据",httpMethod ="POST",response =ResponseData.class )
    @PostMapping("/deleteByResourceNames")
    @ResponseBody
    public ResponseData deleteByResourceNames(@RequestParam List<String> resourceNames){
        ResponseData responseData=new ResponseData();
        int count=0;
        for(String resourceName:resourceNames){
            Boolean result=resourceService.deleteByResourceName(resourceName);
            if(result==true){
                count++;
            }
        }
        responseData.setData(count);
        return responseData;
    }

    @ApiOperation(value = "添加 resource",httpMethod ="GET",response =ResponseData.class )
    @RequestMapping("/addResource")
    @ResponseBody
    public ResponseData addResource(@ApiParam(value = "资源对象，必须要有资源名称属性",required = true)Resource resource){
        ResponseData responseData=new ResponseData();
        //查找数据库中是否有该资源
        ResponseData response=selectByResourceName(resource.getResourceName());
        if(response.getData()!=null){
            responseData.setData("数据库中已经有了该资源");
        }else {
            responseData.setData(resourceService.addResource(resource));
        }
        return responseData;
    }

    @ApiOperation(value = "根据resourceName查找resource",httpMethod ="GET",response =ResponseData.class )
    @RequestMapping("/selectByResourceName")
    @ResponseBody
    public ResponseData selectByResourceName(String resourceName){
        ResponseData responseData=new ResponseData();
        Resource resource=resourceService.selectByResourceName(resourceName);
        responseData.setData(resource);
        return responseData;
    }

}
